/*
 *   Licensed to the Apache Software Foundation (ASF) under one
 *   or more contributor license agreements.  See the NOTICE file
 *   distributed with this work for additional information
 *   regarding copyright ownership.  The ASF licenses this file
 *   to you under the Apache License, Version 2.0 (the
 *   "License"); you may not use this file except in compliance
 *   with the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing,
 *   software distributed under the License is distributed on an
 *   "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 *   KIND, either express or implied.  See the License for the
 *   specific language governing permissions and limitations
 *   under the License.
 *
 */

package tamu.edu.client;


import java.util.List;

import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.Style.Display;
import com.google.gwt.dom.client.TableElement;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Anchor;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;


/**
 * TODO TwitterSignIn.
 *
 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
 */
public class TwitterSignIn extends Composite
{

    private final TweetPackServiceAsync tweetPackService = GWT.create( TweetPackService.class );

    private static TwitterSignInBinder uiBinder = GWT.create( TwitterSignInBinder.class );

    interface TwitterSignInBinder extends UiBinder<Widget, TwitterSignIn>
    {
    }

    private String twitterUrl;

    @UiField
    Anchor lnkTry;

    @UiField
    TableElement tweetTable;

    @UiField
    TextBox txtPin;

    @UiField
    Button btnSubmit;

    @UiField
    TextArea tweets;

    public TwitterSignIn()
    {
        initWidget( uiBinder.createAndBindUi( this ) );
        tweetTable.getStyle().setDisplay( Display.NONE );
        lnkTry.addClickHandler( new ClickHandler()
        {

            public void onClick( ClickEvent event )
            {
                showTwitterLogin();
            }
        } );

        btnSubmit.addClickHandler( new ClickHandler()
        {

            public void onClick( ClickEvent event )
            {
                getTweets();
            }
        } );
    }


    private void getTweets()
    {
        tweetPackService.getTweets( twitterUrl, txtPin.getText(), new AsyncCallback<List<String>>()
        {
            public void onSuccess( List<String> result )
            {
                StringBuilder sb = new StringBuilder();
                for( String s : result )
                {
                    sb.append(s)
                      .append('\n');
                }
                
                tweets.setText( sb.toString() );
            }

            public void onFailure( Throwable caught )
            {
                tweets.setText( caught.getMessage() );
            }
        } );
    }


    private void showTwitterLogin()
    {
        tweetPackService.getReqTokenLink( new AsyncCallback<String>()
        {

            public void onSuccess( String result )
            {
                twitterUrl = result;
                Window.alert( twitterUrl );
                
                Window.open( twitterUrl, "_blank", null );
                tweetTable.getStyle().setDisplay( Display.INLINE );
            }


            public void onFailure( Throwable caught )
            {
                Window.alert( caught.getMessage() );
            }
        } );
    }
}
